if !exists(param.S)

  M291 S5 J1 F250 L150 H450 R"Set Left Tool Temperature" P"Please set the temperature for the filament previously loaded in the Left Tool."
  var ll = input
    
  M291 S5 J1 F{var.ll} L150 H450 R"Set Right Tool Temperature" P"Please set the temperature for the filament previously loaded in the Right Tool."
  var rr = input
  
  G10 P0 R{var.ll} S{var.ll}
  G10 P1 R{var.rr} S{var.rr}
  G10 P2 R{var.ll,var.rr} S{var.ll,var.rr}
  G10 P3 R{var.ll,var.rr} S{var.ll,var.rr}
  
  M568 P0 A2
  M568 P1 A2
  
  M291 S2 R"Please Remove the Build Sheet" P"To avoid damaging your machine, remove the build sheet from the build plate.<br>Click ""OK"" when you are ready to proceed or ""Cancel"" to stop."
  
  if !move.axes[0].homed || !move.axes[1].homed || !move.axes[2].homed || !move.axes[3].homed
    M98 P"0:/sys/homeall.g" S1
  
  G90
  G1 X-999 U999 Y-75 F18000
  
  M116 P0 S10
  M116 P1 S10
  
  T3
  M83                 ; relative extruder moves
  G1 E-50 F{60}*{10}   ; retract filament
  
  M98 P"0:/sys/nozzlewipe.g" C1
  M400
  while iterations < 2
    M98 P"0:/sys/nozzlewipe.g" L1 C1
    M400
  
  G1 F18000 X-50 Y-150 Z125  ; Move tools to the cleaning position
  M291 S2 R"Please Clean Both Nozzles" P"Use a metal brush to clean both nozzles thoroughly to prevent machine damage.<br>Click ""OK"" when you are ready to proceed or ""Cancel"" to stop." S2
  
  M98 P"0:/sys/homeall.g" S1
  
  G29 S2     ; disable Mesh Bed Leveling
  M290 R0 S0
  M208 Z-5 S1
  M558 K0 P8 C"1.io4.in" H5 F100 T18000              ; define Z probe parameters
  M98 P"0:/user/probeoffset.g"                       ; load global variables
  T0
  G1 U999 F18000
  M42 P4 S1                                              ; Turn on relay































M98 P"0:/sys/nozzlewipe.g" T0


var y_test_pos = -101
var add_z_offset = -0.070

; ===== Test diviation =====
M42 P4 S1                ; Turn on relay

G90
G1 X0 Y{var.y_test_pos} Z5 F18000

G91
M98 P"0:/sys/nozzleprobe.g" Z1
var lll = move.axes[2].machinePosition
G1 Z5

echo "Left "^{var.lll}^" mm"


G90
G1 X-999 F18000

M98 P"0:/sys/nozzlewipe.g" T1

G90
G1 X-999 F18000

G1 U{0-global.uoffset} Y{var.y_test_pos - global.yoffset}

G90
G1 Z3
M98 P"0:/sys/nozzleprobe.g" Z10
var rrr = move.axes[2].machinePosition
G1 Z10

M42 P4 S0                ; Turn off relay

G90
G1 X-999 U999 F18000

var ddd = {var.lll - var.rrr}
echo "Right "^{var.rrr}^" mm"


if exists(global.rtzoffset)
  set global.rtzoffset = var.ddd
else
  global rtzoffset = var.ddd


; Generate rtzoffset.g
echo >"0:/user/rtzoffset.g" "if exists(global.rtzoffset)"
echo >>"0:/user/rtzoffset.g" "  set global.rtzoffset = "^{var.ddd}
echo >>"0:/user/rtzoffset.g" "else"
echo >>"0:/user/rtzoffset.g" "  global rtzoffset = "^{var.ddd}

; Generate tooloffset.g
echo >"0:/user/tooloffset.g" "; Set tool offsets"
echo >>"0:/user/tooloffset.g" "G10 P1 U"^{global.uoffset}^" Y"^{global.yoffset}^" Z"^{global.rtzoffset}


if abs(var.ddd) < 0.05 ; ===If delta is less than allowed value===
  echo "Good, deviation is "^{var.ddd}^" mm"
  M291 S1 T10 R"Tool Height calibration successful" P" "

else ; ===If delta is more than allowed value===
  M291 S4 K{"Calibrate","Skip Calibration","Cancel Calibration"} R{"Nozzles are deviated by "^var.ddd^" mm"} P"Please select an option to proceed"
  
  if input == 0 ; User chose "Calibrate"
    M291 S2 R"Calibration Process" P"You will be asked to: Loosen heat break and lower it. The build plate will push the nozzle up to match the other nozzle's level.<br>Click ""OK"" when ready to start."
    ;====Left is Lower====
    if var.ddd > 0
      G90
      G1 U-100 X-999 Z200 F18000
      M400

      
      M291 S2 R"Step 1: Loosen the Heatbreak" P"Use a 2mm hex screwdriver to loosen the heat break of the Right nozzle so the heat block can slide up and down.<br>Caution: HOT!"
      G1S1
      M291 S2 R"Step 2: Lower the Heatblock" P"Lower the heat block 2-3mm (Push it with a screwdriver or pliers)<br>(Caution: HOT!)"
      
      G1 X-999 U{0 - global.uoffset} Y{var.y_test_pos - global.yoffset} F18000

      G90
      G1 Z5 F18000
      G1 Z{var.lll + var.add_z_offset} F240
      
      M400
      M291 S2 R"Tighten Heat Break for Right Tool" P"Securely tighten the heat break after adjustment.<br>Click ""OK"" when done."
      
      G90
      G1 U-100 Z200 F18000

    
    ;====Right is Lower====
    if var.ddd < 0
      G90
      G1 X100 U999 Z200 F18000    
      M400

      M291 S2 R"Step 1: Loosen the Heatbreak" P"Use a 2mm hex screwdriver to loosen the heat break of the Left nozzle so the heat block can slide up and down.<br>Caution: HOT!"
      G1S1
      M291 S2 R"Step 2: Lower the Heatblock" P"Lower the heat block 2-3mm (Push it with a screwdriver or pliers)<br>(Caution: HOT!)"
      
      G1 U999 X0 Y{var.y_test_pos} F18000

      G90
      G1 Z5 F18000
      G1 Z{var.rrr + var.add_z_offset} F240
      
      M400
      M291 S2 R"Tighten Heat Break for Left Tool" P"Securely tighten the heat break after adjustment.<br>Click ""OK"" when done."
      
      G90
      G1 X100 Z200 F18000

    
    M291 S2 R"Confirm Tightening" P"Ensure the heat break is fully tightened to maintain alignment.<br>Click ""OK"" to confirm."
    
    G90
    G1 X-999 U999 F18000
    M98 P"0:/macros/System/Calibration/Auto Calibration Macros/Tool Height Auto Calibration" S1

  
  elif input == 1 ; User chose "Skip Calibration"
    echo "Skipping calibration as per user request."
  
  elif input == 2 ; User chose "Cancel Calibration"
    abort "Calibration cancelled by user."

G90
G1 X-999 U999 F18000







































if !exists(param.S)

  G10 P0 S0 R0
  G10 P1 S0 R0
  G10 P2 S0 R0
  G10 P3 S0 R0
  
  M208 Z0 S1
  M558 K0 P8 C"1.io4.in" H5 F300 T18000
  M98 P"0:/user/probeoffset.g"                       ; load global variables
  
  G90
  G1 X-999 U999 Y150 Z100 F18000